package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Slf4j
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(CacheConstants.ENABLE_NURSING_LEVEL_ALL);
        return 1;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevelMapper.updateById(nursingLevel);
        redisTemplate.delete(CacheConstants.ENABLE_NURSING_LEVEL_ALL);
        return 1;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int result = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        redisTemplate.delete(CacheConstants.ENABLE_NURSING_LEVEL_ALL);
        return result;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        nursingLevelMapper.deleteById(id);
        redisTemplate.delete(CacheConstants.ENABLE_NURSING_LEVEL_ALL);
        return 1;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    @Override
    public List<NursingLevel> getAllByStatus(int i) {
        // 从缓存中查询所有启用护理等级数据
        List<NursingLevel> nursingLevels = null;
        try {
            nursingLevels = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.ENABLE_NURSING_LEVEL_ALL);
        } catch (Exception e) {
            log.error("查询redis缓存失败，{}", e.getMessage(), e);
        }
        if(CollUtil.isNotEmpty(nursingLevels)){
            return nursingLevels;
        }
        // 缓存未命中，从数据库查询
        nursingLevels = this.list(new LambdaQueryWrapper<NursingLevel>().eq(NursingLevel::getStatus, i));
        // 写入redis
        if(CollUtil.isNotEmpty(nursingLevels)){
            try {
                redisTemplate.opsForValue().set(CacheConstants.ENABLE_NURSING_LEVEL_ALL, nursingLevels);
            } catch (Exception e) {
                log.error("写入redis缓存失败，{}", e.getMessage(), e);
            }
        }
        return nursingLevels;
    }
}
